#include "stdafx.h"
#include "ChannelInfoMetaData.h"
#include "DataBasic.h"
#include <Shlwapi.h>

namespace DataManager
{

bool ChannelInfoMetaDataWriter::Init( const std::wstring& file_path ,MessageLoopForIO* io_loop )
{
	io_loop_ = io_loop;
	TCHAR	sz_file_path[MAX_PATH];
	_tcscpy_s(sz_file_path, MAX_PATH, file_path.c_str());
	::PathRenameExtension(sz_file_path, kChannelInfoMetaDataExtension);

	HANDLE file_info_meta_data_handle =  CreateFile( sz_file_path,FILE_APPEND_DATA , 
		FILE_SHARE_READ,NULL, CREATE_ALWAYS, FILE_FLAG_OVERLAPPED ,NULL);
	if(file_info_meta_data_handle == INVALID_HANDLE_VALUE )
		return false;
	CreateFileWriter(file_info_meta_data_handle, io_loop);

	return true;
}

void ChannelInfoMetaDataWriter::CreateFileWriter( HANDLE file_info_meta_data_handle, MessageLoopForIO* io_loop )
{
	channel_info_writer_ = new AsyncFileWriter(file_info_meta_data_handle,io_loop);
}

ChannelInfoMetaDataWriter::~ChannelInfoMetaDataWriter()
{
	channel_info_meta_data_.SerializeToFile(channel_info_writer_);
	channel_info_writer_->EndWriteFile();
}

}